package simulator;

import java.io.FileWriter;
import java.io.IOException;

public class Log {
	private long startTime = 0;
	private String fileName;
	private boolean wordStarted = false;
	
	public Log(String fileName) {
		this.fileName = fileName;
		
		startTime = System.currentTimeMillis();
		try {
			FileWriter fout = new FileWriter(fileName + ".txt", false);
			fout.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	public void log(String string) {
		float timeStamp = (float)(System.currentTimeMillis() - startTime) / 1000;
		try {
			FileWriter fout = new FileWriter(fileName + ".txt", true);
			fout.write(timeStamp + " " + string + "\r\n");
			fout.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	public void newWord() {
		wordStarted = false;
	}
	
	public void startWord(String string) {
		if (wordStarted == false) {
			wordStarted = true;
			if (string.equals("")) {
				log("start");
			} else{
				log("start " + string);
			}
			
		}
	}
}

/*import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import keyboard.Position;

public class Log {
	private int wordCnt;
	private String currWord;
	private String currUser;
	private String currMethod;
	private String currKeyboard;
	private int currLetterIndex;
	private int currOperationCnt;
	private Position currPosition;
	private Position currKeyCentre;
	private Position currKeySize;
	private int currWordPriority;
	private long beginTime;
	private long endTime;
	private boolean during = false;
	public ArrayList<Info> infoList;
	private FileWriter fout;
	
	public Log() {
		currWord = "";
		currUser = "";
		currMethod = "";
		infoList = new ArrayList<>();
	}
	
	class Info {
		String user;
		String method;
		String keyboardShape;
		int wordID;
		String word;
		String operation;
		String suboperation;
		int letterIndex;
		char letter;
		Position lastPosition;
		Position thisPosition;
		float distance;
		Position keyCentre;
		Position keySize;
		long lastEnding;
		long thisBegining;
		long thisEnding;
		float totalDuration;
		float moveDuration;
		int operationCnt;
		int wordPriority;
		
		public Info() {
			this.user = currUser;
			this.method = currMethod;
			this.keyboardShape = currKeyboard;
			this.wordID = wordCnt;
			this.word = currWord;
			this.letterIndex = currLetterIndex;
			this.letter = (currWord != null && letterIndex < currWord.length()) ? currWord.charAt(letterIndex) : '#';
			this.lastPosition = new Position();
			this.thisPosition = currPosition == null ? new Position() : currPosition;
			this.keyCentre = currKeyCentre == null ? new Position() : currKeyCentre;
			this.keySize = currKeySize == null ? new Position() : currKeySize;
			this.thisBegining = beginTime;
			this.thisEnding = endTime;
			this.operationCnt = currOperationCnt;
			this.wordPriority = currWordPriority;
		}
		
		public void input(String line) {
			String[] tokens = line.split(", ");
			this.user = tokens[0];
			this.method = tokens[1];
			this.keyboardShape = tokens[2];
			this.wordID = Integer.parseInt(tokens[3]);
			this.word = tokens[4];
			this.operation = tokens[5];
			this.suboperation = tokens[6];
			this.letterIndex = Integer.parseInt(tokens[7]);
			this.letter = tokens[8].charAt(0);
			this.lastPosition.r = Float.parseFloat(tokens[9]);
			this.lastPosition.c = Float.parseFloat(tokens[10]);
			this.thisPosition.r = Float.parseFloat(tokens[11]);
			this.thisPosition.c = Float.parseFloat(tokens[12]);
			this.distance = Float.parseFloat(tokens[13]);
			this.keyCentre.r = Float.parseFloat(tokens[14]);
			this.keyCentre.c = Float.parseFloat(tokens[15]);
			this.keySize.r = Float.parseFloat(tokens[16]);
			this.keySize.c = Float.parseFloat(tokens[17]);
			this.lastEnding = Long.parseLong(tokens[18]);
			this.thisBegining = Long.parseLong(tokens[19]);
			this.thisEnding = Long.parseLong(tokens[20]);
			this.totalDuration = Float.parseFloat(tokens[21]);
			this.moveDuration = Float.parseFloat(tokens[22]);
			this.operationCnt = Integer.parseInt(tokens[23]);
			this.wordPriority = Integer.parseInt(tokens[24]);	
		}
		
		public void output(String outputFile) {
			String outputString = "";
			outputString += user + ", " + method + ", " + keyboardShape + ", ";
			outputString += wordID + ", " + word + ", " + operation + ", " + suboperation + ", ";
			outputString += letterIndex + ", " + letter + ", ";
			outputString += lastPosition.toString() + ", " + thisPosition.toString() + ", " + distance + ", ";
			outputString += keyCentre.toString() + ", " + keySize.toString() + ", ";
			outputString += lastEnding + ", " + thisBegining + ", " + thisEnding + ", " + totalDuration + ", " + moveDuration + ", ";
			outputString += operationCnt + ", " + wordPriority;
			try {
				fout = new FileWriter(outputFile, true);
				fout.write(outputString + "\n");
				fout.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		public void output() {
			if (infoList.size() > 0) {
				Info lastInfo = infoList.get(infoList.size() - 1);
				lastEnding = lastInfo.thisEnding;
				lastPosition = lastInfo.thisPosition;
				distance = Position.EuclidDist(lastPosition, thisPosition);
				totalDuration = (thisEnding - lastEnding) * 0.001f;
				moveDuration = (thisEnding - thisBegining) * 0.001f;
			}
			output(currUser + "_" + currMethod + "_" + currKeyboard + ".txt");
			infoList.add(this);
		}
	}
	
	public void input(String fileName) {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader(fileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		
		try {
			String line = bufferedReader.readLine();
			while ((line = bufferedReader.readLine()) != null) {
				Info info = new Info();
				info.input(line);
				infoList.add(info);
			}
			bufferedReader.close();
			fileReader.close();
		} catch (IOException exp) {
			exp.printStackTrace();
		}
	}*
	
	public void setUser(String user) {
		this.currUser = user;
	}
	
	public void outputTitle(String outputFile) {
		try {
			fout = new FileWriter(outputFile, false);
			fout.write("user, method, keyboard, wordID, word, " +
					"operation, suboperation, letterIndex, letter, " +
					"lastPositionX, lastPositionY, thisPositionX, thisPositionY, " +
					"distance, keyCentreR, keyCentreC, keySizeH, keySizeW, " +
					"lastEnding, thisBegining, thisEnding, totalDuration, moveDuration, " +
					"operationCnt, wordPriority\n");
			fout.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void outputTitle() {
		outputTitle(currUser + "_" + currMethod + "_" + currKeyboard + ".txt");
	}
	
	public void setMethod(String method) {
		this.currMethod = method;
	}
	
	public void setKeyboardShape(String keyboardShape) {
		this.currKeyboard = keyboardShape;
		outputTitle();
	}
	
	public void addOperationCnt() {
		currOperationCnt++;
	}
	
	public boolean begin() {
		if (during == false) {
			during = true;
			beginTime = System.currentTimeMillis();
			currOperationCnt = 0;
			currPosition = new Position();
			currKeyCentre = new Position();
			currKeySize = new Position();
			currWordPriority = 0;
			return true;
		}
		return false;
	}
	
	private void end() {
		begin();
		during = false;
		endTime = System.currentTimeMillis();
	}
	
	public void setCurrPosition(Position currPosition) {
		this.currPosition = currPosition;
	}
	
	public void setCurrKeyCentre(Position currKeyCentre) {
		this.currKeyCentre = currKeyCentre;
	}
	
	public void setCurrKeySize(Position currKeySize) {
		this.currKeySize = currKeySize;
	}
	
	public void setCurrWordPriority(int currWordPriority) {
		this.currWordPriority = currWordPriority;
	}
	
	public void addWord() {
		wordCnt++;
	}	
	
	public void deleteWord() {
		end();
		wordCnt--;
		Info info = new Info();
		info.operation = "scan";
		info.suboperation = "delete";
		info.output();
	}
	
	public void setCurrWord(String word) {
		currWord = word;
		currLetterIndex = 0;
	}
	
	public void addLetter() {
		end();
		Info info = new Info();
		info.operation = "code";
		info.suboperation = "enter";
		info.output();
		currLetterIndex++;
	}
	
	public void deleteLetter() {
		end();
		if (currLetterIndex >= 0)
			currLetterIndex--;
		Info info = new Info();
		info.operation = "code";
		info.suboperation = "delete";
		info.output();
	}
	
	public void turnLastPage() {
		end();
		Info info = new Info();
		info.operation = "scan";
		info.suboperation = "previous";
		info.output();
	}
	
	public void turnNextPage() {
		end();
		Info info = new Info();
		info.operation = "scan";
		info.suboperation = "next";
		info.output();
	}
	
	public void confirmWord() {
		end();
		Info info = new Info();
		info.operation = "scan";
		info.suboperation = "confirm";
		info.output();
	}
}*/
